<%--
  Created by IntelliJ IDEA.
  User: 十一
  Date: 2021/11/16
  Time: 8:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>QQ注册</title>
    <%--jquery--%>
    <script src="/file/getJquery/3.6.0.0"></script>
    <%--加密使用的包--%>
    <script src="https://cdn.bootcss.com/crypto-js/3.1.9-1/crypto-js.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/aes-js/3.1.2/index.min.js"></script>
</head>
<body>
<form action="#">
    昵称：<input type="text" name="nick">
    密码：<input type="password" name="password">
    手机号：<input type="text" name="telphone">
    验证码：<input type="text" name="smsvc">
    <button type="button" id="submit">立即注册</button>
</form>
<script>
    let aaa = "${key}";
    // AES ECB模式加密，没有iv
    function AES_ECB_encrypt(message, key) {
        // utf8字符串—>WordArray对象，WordArray是一个保存32位整数的数组，相当于转成了二进制
        let keyHex = CryptoJS.enc.Utf8.parse(key); //
        let messageHex = CryptoJS.enc.Utf8.parse(message);
        let encrypted = CryptoJS.AES.encrypt(messageHex, keyHex, {
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        });
        return encrypted.toString();// base64结果
        //return encrypted.ciphertext.toString();   // 二进制结果
    }


    /*ES6以后的代码规范*/
    $("#submit").on("click",function(){
        //当点击了按钮过后获取input值
        let nick = $("input[name='nick']").val();
        //加密方法进行加密
        let password = AES_ECB_encrypt($("input[name='password']").val(),aaa);
        let telphone = AES_ECB_encrypt($("input[name='telphone']").val(),aaa);
        let smsvc = $("input[name='smsvc']").val();
        $.ajax({
            type:"post",//请求类型
            url:"/user/userRegister",//请求地址
            contextType:"application/json",//请求数据类型（方便Spring MVC进行封装）
            dataType:"json",//返回数据类型
            data:{"nick":nick,"password":password,"telphone":telphone,"smsvc":smsvc},//请求携带数据
            success:function(data){
                //成功时回调
                console.log("请求成功",data);
            },
            error:function(err){
                //失败时回调
                console.log("请求失败",err);
            }
        });
        console.log(nick);
        console.log(password);
        console.log(telphone);
        console.log(smsvc);
    });
</script>
</body>
</html>
